﻿<template>
  <div class="wrapper">
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>系统管理</el-breadcrumb-item>
      <el-breadcrumb-item>修改密码</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card style="width:480px">
      <div slot="header" class="clearfix" >
      <span style="font-size:20px">修改密码</span>
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" label-position='top' class="demo-ruleForm">
        <el-form-item label="原密码" prop="oldPass" >
        <el-input v-model="ruleForm.oldPass" placeholder="请输入原密码"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="newPass">
        <el-input type="password" v-model="ruleForm.newPass" autocomplete="off" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="checkPass">
        <el-input type="password" v-model="ruleForm.checkPass" autocomplete="off" placeholder="请重复新密码"></el-input>
      </el-form-item>
     
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
      
    </el-card>
  </div>
</template>

<script>
import {modifyPwd} from '@/api/user.js'
export default {
  components: {},
  props: {},
  data() {
    var checkOldPass = (rule, value, callback) => {
        if (!value) {
          return callback(new Error('原密码不能为空'));
        }
        
      };
      var validatePass = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请输入新密码'));
        } else {
          if (this.ruleForm.checkPass !== '') {
            this.$refs.ruleForm.validateField('checkPass');
          }
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === '') {
          callback(new Error('请再次输入密码'));
        } else if (value !== this.ruleForm.newPass) {
          callback(new Error('两次输入密码不一致!'));
        } else {
          callback();
        }
      };
      return {
        ruleForm: {
          newPass: '',
          checkPass: '',
          oldPass: ''
        },
        rules: {
          newPass: [
            { validator: validatePass, trigger: 'blur' }
          ],
          checkPass: [
            { validator: validatePass2, trigger: 'blur' }
          ],
          oldPass: [
            { validator: checkOldPass, trigger: 'blur' }
          ]
        }
      };
  },
  watch: {},
  computed: {},
  methods: {
    submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
              let user=JSON.stringify(sessionStorage.getItem('userInfo'))
            // 原密码是否正确
            let temp=user.pwd;
            if(this.ruleForm.oldPass!=temp){
               return this.$message.Error('原密码输入错误！')
            }
          modifyPwd(user.userName,this.ruleForm.newPass).then(res=>{
            this.$message.success('修改成功！')
          })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    
  },
  created() {},
  mounted() {}
};
</script>
<style lang="less" scoped>
// .wrapper{}
.el-form{
  margin-top: 10px;
}
</style>